import { BasicColumn } from "/@/components/Table"
import { FormSchema } from "/@/components/Table"
import { listDataGroupTree } from "/@/api/infra/dataGroup"
import { DataGroupBizType } from "/@/components/BizDataGroup/index"
import { render } from "/@/utils/common/renderUtils"
import { rules } from "/@/utils/helper/validator"
export const columns: BasicColumn[] = [
  {
    title: "字典名称",
    dataIndex: "dictName",
    width: 240,
  },
  {
    title: "字典编号",
    dataIndex: "dictCode",
    width: 240,
  },
  {
    title: "字典组",
    dataIndex: "groupName",
    width: 160,
  },
  {
    title: "描述",
    dataIndex: "remark",
    width: 300,
  },
]

export const recycleBincolumns: BasicColumn[] = [
  {
    title: "字典名称",
    dataIndex: "dictName",
    width: 120,
  },
  {
    title: "字典编号",
    dataIndex: "dictCode",
    width: 120,
  },
  {
    title: "字典组",
    dataIndex: "dictGroup",
    width: 120,
  },
  {
    title: "描述",
    dataIndex: "remark",
    width: 120,
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    label: "字典名称",
    field: "dictName",
    component: "Input",
    colProps: { span: 6 },
  },
  {
    label: "字典编码",
    field: "dictCode",
    component: "Input",
    colProps: { span: 6 },
  },
  {
    label: "字典组",
    field: "groupId",
    component: "ApiSelect",
    componentProps: {
      api: listDataGroupTree,
      params: {
        bizType: DataGroupBizType.DICT,
      },
      optionFilterProp: "label",
      showSearch: true,
      labelField: "name",
      valueField: "id",
    },
    colProps: { span: 6 },
  },
]

export const formSchema: FormSchema[] = [
  {
    label: "",
    field: "id",
    component: "Input",
    show: false,
  },
  {
    label: "字典组",
    field: "groupId",
    component: "ApiSelect",
    componentProps: {
      api: listDataGroupTree,
      params: {
        bizType: DataGroupBizType.DICT,
      },
      optionFilterProp: "label",
      showSearch: true,
      labelField: "name",
      valueField: "id",
      dropdownStyle: { maxHeight: "16vh", overflow: "auto" },
    },
  },
  {
    label: "字典名称",
    field: "dictName",
    required: true,
    component: "Input",
  },
  {
    label: "字典编码",
    field: "dictCode",
    required: true,
    component: "Input",
    componentProps: {
      placeholder: "建议统一命名风格, 比如大驼峰命名方式, eg: ResourceShowScope",
    },
    dynamicDisabled: ({ values }) => {
      return !!values.id
    },
    // dynamicRules:({model,schema}) =>rules.duplicateCheckRule("sys_dict", "dict_code",model,schema,true),
  },
  {
    label: "描述",
    field: "remark",
    component: "InputTextArea",
    componentProps: {
      autoSize: {
        minRows: 5,
        maxRows: 6,
      },
    },
  },
]

export const dictItemColumns: BasicColumn[] = [
  {
    title: "名称",
    dataIndex: "label",
    width: 120,
  },
  {
    title: "数据值",
    dataIndex: "value",
    width: 60,
  },
  {
    title: "状态",
    dataIndex: "status",
    width: 60,
    customRender: ({ text }) => {
      let color = "green"
      if (text == "2") color = "red"

      return render.renderTag(render.renderDict(text, "dict_item_status"), color)
    },
  },

  {
    title: "排序编号",
    dataIndex: "sortNo",
    width: 40,
  },
]

export const dictItemSearchFormSchema: FormSchema[] = [
  {
    label: "名称",
    field: "itemLabel",
    component: "Input",
  },
  {
    label: "状态",
    field: "status",
    component: "JDictSelectTag",
    componentProps: {
      dictType: "dict_item_status",
      stringToNumber: true,
    },
  },
]

export const itemFormSchema: FormSchema[] = [
  {
    label: "",
    field: "id",
    component: "Input",
    show: false,
  },
  {
    label: "名称",
    field: "label",
    required: true,
    component: "Input",
  },
  {
    label: "数据值",
    field: "value",
    component: "Input",
    dynamicRules: ({ values, model }) => {
      return [
        {
          required: true,
          validator: (_, value) => {
            if (!value) {
              return Promise.reject("请输入数据值")
            }
            // if (new RegExp("[`~!@#$^&*()=|{}'<>《》?！￥（）—【】‘；：”“。，、？]").test(value)) {
            //   return Promise.reject('数据值不能包含特殊字符！');
            // }
            return new Promise<void>((resolve, reject) => {
              // let params = {
              //   dictId: values.dictId,
              //   id: model.id,
              //   itemValue: value
              // };
              // dictItemCheck(params)
              //   .then((res) => {
              //     res.success ? resolve() : reject(res.message || '校验失败');
              //   }).catch((err) => {
              //   reject(err.message || '验证失败');
              // });
              return resolve()
            })
          },
        },
      ]
    },
  },
  {
    label: "描述",
    field: "remark",
    component: "Input",
  },
  {
    field: "sortNo",
    label: "排序",
    component: "InputNumber",
    defaultValue: 1,
  },
  {
    field: "status",
    label: "是否启用",
    defaultValue: "1",
    component: "JDictSelectTag",
    componentProps: {
      type: "radio",
      dictType: "dict_item_status",
    },
  },
]
